package com.dsteiner.xelavos.client;




import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.ListBoxWidget;
import com.dsteiner.widgets.client.PaginationWidget;
import com.dsteiner.widgets.client.PopupOverlayWidget;
import com.dsteiner.widgets.client.SuggestBoxWidget;
import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.IWidgetBuilder;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.ScrollPanelExt;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PaginationModel;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.widgets.client.model.SuggestBoxModel;
import com.dsteiner.xelavos.client.SavedSearchWidget.SavedSearchModel;
import com.dsteiner.xelavos.client.SearchInfoWidget.SearchInfoModel;
import com.dsteiner.xelavos.client.SendMessageWidget.SendMessageModel;
import com.dsteiner.xelavos.client.controller.CreateJobController;
import com.dsteiner.xelavos.client.controller.CreateLeisureController;
import com.dsteiner.xelavos.client.controller.CreateProductController;
import com.dsteiner.xelavos.client.controller.CreateServiceController;
import com.dsteiner.xelavos.client.controller.SearchController;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.CreateObjectModel;
import com.dsteiner.xelavos.client.model.DisplayModel;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.SearchModel;
import com.dsteiner.xelavos.client.model.TopNewModel;
import com.dsteiner.xelavos.client.resources.XelavosImageBundle;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SearchWidget extends LazyWidget implements IWidgetBuilder{

	private SearchController searchController = new SearchController();
	private final SearchModel searchModel;
	private final AuthenticationModel authenticationModel;
	private final MainTabModel mainTabModel;
	private final TopNewModel topNewModel;
	
	public SearchWidget(SearchModel shoppingModel, AuthenticationModel authenticationModel, 
			MainTabModel mainTabModel, TopNewModel topNewModel) {
		this.searchModel = shoppingModel;
		this.authenticationModel = authenticationModel;
		this.mainTabModel = mainTabModel;
		this.topNewModel = topNewModel;
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		FlowPanel searchPanel = new FlowPanel();
		SuggestBoxModel suggestBoxModel = new SuggestBoxModel();
		SuggestBoxWidget suggestBoxWidget = new SuggestBoxWidget(suggestBoxModel, 
				new SelectedTabSuggestBoxDataProvider(mainTabModel));
		ImageExt searchButton = new ImageExt(XelavosImageBundle.Util.getInstance().searchIcon().createImage());
		ScrollPanelExt scrollPanel = new ScrollPanelExt();
		FlowPanelExt contentPanel = new FlowPanelExt();
		ImageExt createObjectButton = new ImageExt(XelavosImageBundle.Util.getInstance().createIcon().createImage());
		ImageExt savedSearchIcon = new ImageExt(XelavosImageBundle.Util.getInstance().savedSearchIcon().createImage());
		PopupOverlayModel createObjectPopupOverlayModel = new PopupOverlayModel();
		CreateObjectModel createObjectModel = new CreateObjectModel();
		CreateObjectWidget createObjectWidget = new CreateObjectWidget(createObjectModel, authenticationModel);
		createObjectPopupOverlayModel.setContentWidget(createObjectWidget);
		PopupOverlayWidget createProductPopup = new PopupOverlayWidget(createObjectPopupOverlayModel);
		CreateProductController createProductController = new CreateProductController();
		CreateProductWidget createProductWidget = new CreateProductWidget(createProductController, 
				createObjectModel, mainTabModel);
		CreateJobController createJobController = new CreateJobController();
		CreateJobWidget createJobWidget = new CreateJobWidget(createJobController, 
				createObjectModel, mainTabModel);
		CreateLeisureController createLeisureController = new CreateLeisureController();
		CreateLeisureWidget createLeisureWidget = new CreateLeisureWidget(createLeisureController, 
				createObjectModel, mainTabModel);
		CreateServiceController createServiceController = new CreateServiceController();
		CreateServiceWidget createServiceWidget = new CreateServiceWidget(createServiceController, 
				createObjectModel, mainTabModel);
		PaginationModel paginationModel = new PaginationModel();
		PaginationWidget paginationWidget = new PaginationWidget(paginationModel);
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		ListBoxWidget sortListBox = new ListBoxWidget();
		HTMLExt sortHtml = new HTMLExt("<strong>ordenar por:</strong>");
		HTMLExt createObjectLink = new HTMLExt("<a href=\"javascript:void(0);\">publiquelo</a>");
		HTMLExt todaysLeisureLink = new HTMLExt("<a href=\"javascript:void(0);\">buscar recreaciones de hoy</a>");
		SavedSearchModel savedSearchModel = new SavedSearchModel();
		SavedSearchWidget savedSearchWidget = new SavedSearchWidget(savedSearchModel, authenticationModel);
		PopupOverlayModel savedSearchPopupModel = new PopupOverlayModel();
		savedSearchPopupModel.setContentWidget(savedSearchWidget);
		PopupOverlayWidget savedSearchPopup = new PopupOverlayWidget(savedSearchPopupModel);
		
		SearchInfoModel searchInfoModel = new SearchInfoModel();
		SearchInfoWidget searchInfoWidget = new SearchInfoWidget(searchInfoModel, mainTabModel);
		
		savedSearchIcon.setTitle("Guardar busqueda");
		
		horizontalPanel.setSpacing(5);
		horizontalPanel.add(todaysLeisureLink);
		horizontalPanel.add(paginationWidget);
		horizontalPanel.add(createObjectLink);
		horizontalPanel.add(sortHtml);
		horizontalPanel.add(sortListBox);
		horizontalPanel.add(savedSearchIcon);
		
		searchPanel.add(suggestBoxWidget);
		searchPanel.add(searchButton);
		searchPanel.add(createObjectButton);
		
		mainPanel.add(searchPanel);
		mainPanel.add(horizontalPanel);
		mainPanel.add(searchInfoWidget);
		mainPanel.add(scrollPanel);
//		mainPanel.add(paginationWidget);
		scrollPanel.setWidget((Widget)contentPanel);

		suggestBoxWidget.addStyleName(XelavosResources.INSTANCE.css().searchTextBox());
		setStyleName(XelavosResources.INSTANCE.css().searchWidget());
		searchButton.addStyleName(XelavosResources.INSTANCE.css().searchButton());
		createObjectButton.addStyleName(XelavosResources.INSTANCE.css().createObjectButton());
		searchPanel.addStyleName(XelavosResources.INSTANCE.css().headerPanel());
		scrollPanel.addStyleName(XelavosResources.INSTANCE.css().scrollPanel());
		savedSearchIcon.getElement().getStyle().setCursor(Cursor.POINTER);
		
		PopupOverlayModel sendMessagePopupOverlayModel = new PopupOverlayModel();
		PopupOverlayWidget sendMessagePopupOverlayWidget = new PopupOverlayWidget(sendMessagePopupOverlayModel);
		SendMessageModel sendMessageModel = new SendMessageModel();
		SendMessageWidget sendMessageWidget = new SendMessageWidget(sendMessageModel, authenticationModel);
		sendMessagePopupOverlayModel.setContentWidget(sendMessageWidget);
		
		searchController.registerSendMessagePopupOverlayModel(sendMessagePopupOverlayModel);
		searchController.registerSendMessageModel(sendMessageModel);
		searchController.registerSearchWidget(this);
		searchController.registerSearchModel(searchModel);
		searchController.registerWidgetBuilder(this);
		searchController.registerJsUtil(JsUtil.getInstance());
		searchController.registerEventManager(EventManagerImpl.getInstance());
		searchController.registerSearchTextBoxModel(suggestBoxModel);
		searchController.registerSearchButton(searchButton);
		searchController.registerScrollPanel(scrollPanel);
		searchController.registerContentPanel(contentPanel);
		searchController.registerSavedSearchIcon(savedSearchIcon);
		searchController.registerSavedSearchPopupModel(savedSearchPopupModel);
		searchController.registerCreateObjectButton(createObjectButton);
		searchController.registerCreateObjectOverlayPopupModel(createObjectPopupOverlayModel);
		searchController.registerCreateObjectModel(createObjectModel);
		searchController.registerAuthenticationModel(authenticationModel);
		searchController.registerMainTabModel(mainTabModel);
		searchController.registerCreateProductWidget(createProductWidget);
		searchController.registerCreateJobWidget(createJobWidget);
		searchController.registerCreateLeisureWidget(createLeisureWidget);
		searchController.registerCreateObjectWidget(createObjectWidget);
		searchController.registerCreateLeisureController(createLeisureController);
		searchController.registerCreateJobController(createJobController);
		searchController.registerCreateProductController(createProductController);
		searchController.registerCreateServiceController(createServiceController);
		searchController.registerCreateServiceWidget(createServiceWidget);
		searchController.registerPaginationModel(paginationModel);
		searchController.registerPaginationWidget(paginationWidget);
		searchController.registerSortListBox(sortListBox);
		searchController.registerSortHtml(sortHtml);
		searchController.registerCreateObjectLink(createObjectLink);
		searchController.registerTodaysLeisureLink(todaysLeisureLink);
		searchController.registerSearchInfoWidget(searchInfoWidget);
		return mainPanel;
	}

	public IWidget createWidget(Object model) {
		DisplayWidget userWidget = new DisplayWidget((DisplayModel) model, 
				authenticationModel);
		return userWidget;
	}

}
